package com.fx.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.eduservice.entity.vo.EduChapter;
import com.fx.eduservice.entity.vo.EduCourse;
import com.fx.eduservice.entity.vo.EduVideo;
import com.fx.eduservice.mapper.EduChapterMapper;
import com.fx.eduservice.response.chapter.OneChapter;
import com.fx.eduservice.response.chapter.TwoVideo;
import com.fx.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fx.eduservice.service.EduVideoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-07
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    final private EduVideoService eduVideoService;

    @Override
    public ReturnMessage<Object> getChapterVideo(String couseId) {


        //查询所有章的课程信息
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.select("id", "course_id", "title").eq(StringUtils.isNotBlank(couseId), "course_id", couseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterQueryWrapper);


        //查询所有节的课程信息
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.select("id", "course_id", "chapter_id", "title").eq(StringUtils.isNotBlank(couseId), "course_id", couseId);
        List<EduVideo> eduVideos = eduVideoService.list(videoQueryWrapper);

        //封装到返回集合中
        List<OneChapter> oneChapters = new ArrayList<>();
        for (EduChapter eduChapter : eduChapters
        ) {
            OneChapter oneChapter = new OneChapter();
            BeanUtils.copyProperties(eduChapter, oneChapter);

            for (EduVideo eduVideo : eduVideos
            ) {
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    TwoVideo twoVideo = new TwoVideo();
                    BeanUtils.copyProperties(eduVideo, twoVideo);
                    oneChapter.getChildren().add(twoVideo);
                }
            }
            oneChapters.add(oneChapter);

        }


        return ReturnMessageUtils.success(oneChapters);
    }

    @Override
    public ReturnMessage<Object> deleteById(EduChapter eduChapter) {
        //查询当前章节下是否有节点信息

        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id", eduChapter.getId());
        int eduv = eduVideoService.count(eduVideoQueryWrapper);
        if (eduv > 0) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_EDU_CHAPTER);

        }
        this.removeById(eduChapter.getId());


        return ReturnMessageUtils.success();
    }

    @Override
    public void deleteChapterByCoursmId(String id) {
        QueryWrapper<EduChapter> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",id);
        this.remove(eduVideoQueryWrapper);
    }
}
